package org.stvd.common.security;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.PostConstruct;

import org.activiti.engine.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.stvd.common.Consts;
import org.stvd.common.enums.UserLoginTypeEnum;
import org.stvd.common.security.support.UserDetail;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.admin.Department;
import org.stvd.entities.admin.Roles;
import org.stvd.entities.admin.UserBase;
import org.stvd.entities.admin.UserLogin;
import org.stvd.entities.admin.Users;
import org.stvd.service.admin.DepartmentService;
import org.stvd.service.admin.RolesService;
import org.stvd.service.admin.UsersService;

@Component
public class SecurityUserHolder {

	@Autowired
	private UsersService usersService;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private RolesService rolesService;
	@Autowired
	protected RepositoryService repositoryService;

	// 声明静态变量，为了之后调用service
	public static SecurityUserHolder securityUserHolder;

	@PostConstruct
	public void init() {
		securityUserHolder = this;
		securityUserHolder.usersService = this.usersService;
		securityUserHolder.departmentService = this.departmentService;
		securityUserHolder.rolesService = this.rolesService;
		securityUserHolder.repositoryService = this.repositoryService;
	}

	public static UserDetail getCurrentUserDetail() {
		try {
			return (UserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 获取登录用户信息
	 */
	public static Users getCurrentUserInfo() {
		try {
			return getCurrentUserDetail().getUsers();
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 获取当前登录用户ID
	 * 
	 * @return
	 */
	public static String getCurrentUserId() {
		try {
			return getCurrentUserDetail().getUsers().getUserId();
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 获取当前用户认证类型
	 */
	public static String getUserAuthType() {
		String authType = "-1";// 未认证
		if (getCurrentUserInfo() != null) {
			UserBase userBase = getCurrentUserInfo().getUserBase();
			if (userBase != null) {
				authType = userBase.getAuthType();
			}
		}
		return authType;
	}

	/**
	 * 获取用户账号列表
	 * 
	 * @return
	 */
	public static List<UserLogin> getUserLoginAccount() {
		List<UserLogin> userLoginList = new ArrayList<UserLogin>();
		if (getCurrentUserInfo() != null) {
			userLoginList = getCurrentUserInfo().getUserLoginList();
		}
		return userLoginList;
	}

	/**
	 * 根据账号类型获取用户账号
	 * 
	 * @param loginType 账号类型
	 * @return
	 */
	public static String getUserLoginAccount(String loginType) {
		String loginAccount = "";
		if (StringUtil.isEmpty(loginType)) {
			return loginAccount;
		}
		List<UserLogin> userLoginList = getUserLoginAccount();
		if (userLoginList != null && userLoginList.size() > 0) {
			for (UserLogin userLogin : userLoginList) {
				if (loginType.equals(userLogin.getLoginType())) {
					loginAccount = userLogin.getLoginAccount();
					break;
				}
			}
		}
		return loginAccount;
	}

	/**
	 * 获取用户登录手机号
	 * 
	 * @return
	 */
	public static String getUserLoginMobile() {
		return getUserLoginAccount(UserLoginTypeEnum.Mobile.getValue());
	}

	/**
	 * 获取用户当前登录账号类型
	 * 
	 * @return
	 */
	public static String getUserLoginAccountType() {
		return SecurityUserHolder.getCurrentUserInfo().getAccountType();
	}

	/**
	 * 获取当前登录用户角色列表
	 * 
	 * @return
	 */
	public static List<Roles> getCurrentRoleInfo(String guid, String depId) {
		try {
			List<Roles> roleList = getCurrentRoleInfo(guid);
			if (roleList != null && roleList.size() > 0) {
				Iterator<Roles> it = roleList.iterator();
				while (it.hasNext()) {
					Roles role = it.next();
					if (!depId.equals(role.getDepId())) {
						it.remove();
					}
				}
			}
			return roleList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取当前登录用户角色列表
	 * 
	 * @return
	 */
	public static List<Roles> getCurrentRoleInfo(String guid) {
		try {
			if (StringUtil.isEmpty(guid)) {
				guid = Consts.GUID;
			}
			List<Roles> roleList = new ArrayList<Roles>();
			List<Roles> tmpList = getCurrentUserInfo().getRoleList();
			if (tmpList != null && tmpList.size() > 0) {
				roleList = new ArrayList<>();
				for (Roles roles : tmpList) {
					if (guid.equals(roles.getGuid())) {
						roleList.add(roles);
					}
				}
				return roleList;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取当前登录用户所属机构列表
	 * 
	 * @return
	 */
	public static List<Department> getCurrentDepInfo(String guid) {
		try {
			if (StringUtil.isEmpty(guid)) {
				guid = Consts.GUID;
			}
			List<Department> departmentList = new ArrayList<Department>();
			List<Department> tmpList = getCurrentUserInfo().getDepartmentList();
			if (tmpList != null && tmpList.size() > 0) {
				departmentList = new ArrayList<>();
				for (Department department : tmpList) {
					if (guid.equals(department.getGuid())) {
						departmentList.add(department);
					}
				}
				return departmentList;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取当前用户角色编码串
	 * 
	 * @return
	 */
	public static String getRoleCodes(String guid) {
		String roleCodes = "";
		List<Roles> roleList = getCurrentRoleInfo(guid);
		if (roleList != null && roleList.size() > 0) {
			for (Roles role : roleList) {
				if ("".equals(roleCodes)) {
					roleCodes = role.getRoleCode();
				} else {
					roleCodes += "," + role.getRoleCode();
				}
			}
		}
		return roleCodes;
	}

	/**
	 * 获取当前用户机构编码串
	 * 
	 * @return
	 */
	public static String getDepCodes(String guid) {
		String depCodes = "";
		List<Department> departmentList = getCurrentDepInfo(guid);
		if (departmentList != null && departmentList.size() > 0) {
			for (Department department : departmentList) {
				if ("".equals(depCodes)) {
					depCodes = department.getDepCode();
				} else {
					depCodes += "," + department.getDepCode();
				}
			}
		}
		return depCodes;
	}

	/**
	 * 获取当前用户最大权限机构
	 * 
	 * @return
	 */
	public static Department getCurrentMaxAuthDep() {
		return getCurrentMaxAuthDep(Consts.GUID);
	}

	public static Department getCurrentMaxAuthDep(String guid) {
		Department maxAuthDep = null;
		List<Department> departmentList = getCurrentDepInfo(guid);
		if (departmentList != null && departmentList.size() > 0) {
			for (Department department : departmentList) {
				if (maxAuthDep == null) {
					maxAuthDep = department;
					continue;
				}
				if (maxAuthDep.getInnerCode().length() > department.getInnerCode().length()) {
					maxAuthDep = department;
				}
			}
		}
		return maxAuthDep;
	}

	/**
	 * 获取当前用户最大权限机构ID
	 * 
	 * @return
	 */
	public static String getCurrentMaxAuthDepId() {
		return getCurrentMaxAuthDepId(Consts.GUID);
	}

	public static String getCurrentMaxAuthDepId(String guid) {
		String maxAuthDepId = "";
		Department department = getCurrentMaxAuthDep(guid);
		if (department != null) {
			maxAuthDepId = department.getDepId();
		}
		return maxAuthDepId;
	}
	
	/**
	 * 获取当前用户最小权限机构
	 * 
	 * @return
	 */
	public static Department getCurrentMinAuthDep() {
		return getCurrentMaxAuthDep(Consts.GUID);
	}

	public static Department getCurrentMinAuthDep(String guid) {
		Department minAuthDep = null;
		List<Department> departmentList = getCurrentDepInfo(guid);
		if (departmentList != null && departmentList.size() > 0) {
			for (Department department : departmentList) {
				if (minAuthDep == null) {
					minAuthDep = department;
					continue;
				}
				if (minAuthDep.getInnerCode().length() < department.getInnerCode().length()) {
					minAuthDep = department;
				}
			}
		}
		return minAuthDep;
	}
}
